<?php

namespace app\api\controller;

use app\admin\model\user\Card;
use app\admin\model\user\Extend;
use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Transuser extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function checkout()
    {
        $oldUserNickname = input('old');
        $newUserNickname = input('new');
        $passwd = input('passwd', '');

        if ('ucuew5235sd' != $passwd) {
            $this->error('密码不对');
        }

        $sql = "SELECT * FROM `dancestudio_firs`.`fa_user` WHERE `nickname` like '%$oldUserNickname%'";
        $data = Db::query($sql);

        if (1 == count($data)) {
            $oldUser = $data[0];
            echo $oldUser['nickname'].'<br />';

            $sql = "SELECT * FROM `dancestudio_firs`.`fa_user_extend` WHERE `user_id` = " .$oldUser['id'];
            $data = Db::query($sql);
            echo '余额为:' . $data[0]['balance'] . '<br />';

            $sql = "SELECT * FROM `dancestudio_firs`.`fa_user_card` WHERE `user_id` = ". $oldUser['id'];
            $cards = Db::query($sql);
            foreach ($cards as $card) {
                echo $card['name'] . '-' . $card['total_hour'] . '-' . $card['hasconsumedhour'] . '<br />';
            }
        }

        $newUser = \app\admin\model\User::where('nickname', 'like', "%$newUserNickname%")->select();

        if (1 == count($newUser)) {
            var_dump($newUser[0]->toArray());
        }

        echo '<br />'.'=======' . '<br />';

        echo $oldUser['id'];

        echo '<br />'.'=======' . '<br />';

        echo $newUser[0]->id;


        exit;


    }

    public function trans()
    {
        $userOldId = input('old');
        $userNewId = input('new');
        $passwd = input('passwd', '');

        if ('ucuew5235sd' != $passwd) {
            $this->error('密码不对');
        }

        $sql = "SELECT * FROM `dancestudio_firs`.`fa_user_card` WHERE `user_id` = $userOldId";
        $data = Db::query($sql);

        foreach ($data as $item) {
            Card::create([
                'user_id' => $userNewId,
                'card_id' => $item['card_id'],
                'name' => $item['name'],
                'type' => $item['type'],
                'price' => $item['price'],
                'service_start' => $item['service_start'],
                'service_end' => $item['service_end'],
                'store_ids' => $item['room_id'],
                'checknum' => $item['checknum'],
                'hascheckednum' => $item['hascheckednum'],
                'total_hour' => $item['total_hour'],
                'hasconsumedhour' => $item['hasconsumedhour'],
                'dzmt' => $item['dzmt'],
                'dzmt_code' => null,
                'transaction_id' => $item['transaction_id'],
                'createtime' => $item['createtime'],
                'updatetime' => $item['updatetime']
            ]);
        }

        $sql = "select * from `dancestudio_firs`.`fa_user_extend` WHERE `user_id` = $userOldId";
        $data = Db::query($sql);

        if (isset($data[0]) and isset($data[0]['balance'])) {
            $userExtend = Extend::where('user_id', $userNewId)->find();
            $userExtend->balance = $data[0]['balance'];
            $userExtend->save();
        }

        $this->success('ok');
    }

    public function checkpost()
    {
        $userNewId = input('new');
        $passwd = input('passwd', '');

        if ('ucuew5235sd' != $passwd) {
            $this->error('密码不对');
        }

        $userExtend = Extend::where('user_id', $userNewId)->find();
        echo '余额:' . $userExtend->balance;
        echo '<br />';

        $kk = Card::where('user_id', $userNewId)->select();
        foreach ($kk as $item) {
            echo $item->name . '-' . $item->total_hour . '-' . $item->hasconsumedhour . '<br />';
        }
        exit;

    }
}
